import 'package:flutter/material.dart';
import 'package:im/Pages/chatDetail.dart';
import 'package:im/Provider/chatListProvider.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'im',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: Text("im"),
            actions: [
              IconButton(onPressed: () {}, icon: Icon(Icons.search)),
              IconButton(onPressed: () {}, icon: Icon(Icons.add_circle_outline))
            ],
          ),
          body: MultiProvider(
            providers: [
              ChangeNotifierProvider(
                create: (BuildContext context) => ChatListProvider(),
              ),
            ],
            child: ChatList(),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: [
              BottomNavigationBarItem(
                icon: Icon(
                  Icons.chat_bubble_outline,
                  color: Colors.black,
                ),
                label: "聊天",
                activeIcon: Icon(
                  Icons.chat_bubble,
                  color: Theme.of(context).primaryColor,
                ),
              ),
              BottomNavigationBarItem(
                icon: Icon(
                  Icons.perm_identity,
                  color: Colors.black,
                ),
                label: "我的",
                activeIcon: Icon(
                  Icons.chat_bubble,
                  color: Theme.of(context).primaryColor,
                ),
              ),
            ],
          ),
        ));
  }
}

class ChatList extends StatelessWidget {
  const ChatList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ChatListProvider provider = Provider.of<ChatListProvider>(context);
    if (provider == null) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    return ListView.builder(
        shrinkWrap: true,
        itemCount: provider.chats.length,
        itemBuilder: (BuildContext context, int index) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              GestureDetector(
                onTap: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => Detailpage()));
                },
                child: ListTile(
                  leading:
                      Image.network(provider.chats[index].userIds[0].avatarUrl),
                  title: Text(provider.chats[index].userIds[0].userName),
                  subtitle: Text(provider.chats[index].lastContent),
                  trailing: Text(provider.chats[index].lastUpdateTime),
                ),
              ),
              const Divider()
            ],
          );
        });
  }
}
